from appium.webdriver.common.appiumby import AppiumBy

from base.wecom_app import WeComApp
from utils.log_utils import logger


class ManualInputPage(WeComApp):
    # 完整输入/快速输入
    _INPUT_TYPE = AppiumBy.XPATH, "//*[contains(@text,'输入')]"
    # 姓名输入框
    _NAME_INPUT = AppiumBy.XPATH, "//*[@text='姓名']/following-sibling::*[1]"
    # 手机号输入框
    _PHONENUM_INPUT = AppiumBy.XPATH, "//*[@text='手机']/following-sibling::*[1]//android.widget.EditText"
    # 取消发送邀请通知
    _SEND_MESSAGE = AppiumBy.XPATH, '//*[@text="保存后自动发送邀请通知"]'
    # 保存按钮
    _SAVE_BTN = AppiumBy.XPATH, '//*[@text="保存"]'

    def input_member_info(self, name, phone):
        from page.add_member_page import AddMemberPage
        # 如果处于“完整输入”页面，点击右上角进入快速输入
        ele_input_type = self.find(*self._INPUT_TYPE)

        if ele_input_type.text == '快速输入':
            ele_input_type.click()

        # 输入成员信息
        logger.info(f"输入姓名{name}")
        self.find_and_sendkeys(name, *self._NAME_INPUT)
        logger.info(f"输入手机号{phone}")
        self.find_and_sendkeys(phone, *self._PHONENUM_INPUT)

        logger.info("点击【取消发送邀请通知】")
        self.find_and_click(*self._SEND_MESSAGE)

        logger.info("点击【保存】")
        self.find_and_click(*self._SAVE_BTN)

        # 跳转添加成员页面
        return AddMemberPage(self.driver)

    def input_member_info_fail(self, name, phone, tip):
        # 如果处于“完整输入”页面，点击右上角进入快速输入
        ele_input_type = self.find(*self._INPUT_TYPE)

        if ele_input_type.text == '快速输入':
            ele_input_type.click()

        # 输入成员信息
        logger.info(f"输入姓名{name}")
        self.find_and_sendkeys(name, *self._NAME_INPUT)
        logger.info(f"输入手机号{phone}")
        self.find_and_sendkeys(phone, *self._PHONENUM_INPUT)

        logger.info("点击【取消发送邀请通知】")
        self.find_and_click(*self._SEND_MESSAGE)

        logger.info("点击【保存】")
        self.find_and_click(*self._SAVE_BTN)

        logger.info(f"返回提示信息{tip}")
        return self.wait_for_text(tip)
